package com.sgb.kjwl.view.ui.fragment.shop;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopJicaiChildBinding;
import com.sgb.kjwl.di.shopping.DaggerShoppingFragComponent;
import com.sgb.kjwl.di.shopping.ShoppingFragModule;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.home.ShopJiCaiAdapter;
import com.sgb.kjwl.viewmodel.shopping.ShopJcChildModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.List;
import javax.inject.Inject;

/**
 * 企业店铺：集采管理
 */
public class ShopJcChildFragment extends AppLazyFragment {
    public static final String Company_No = "company_No";
    private FragmentShopJicaiChildBinding binding;
    private ShopJiCaiAdapter adapter;
    @Inject
    ShopJcChildModel viewModel;
    private JiCaiHomeListRequestBean listBean;
    private int topnum = 1;
 //   private ShopJiCaiAdapter<FactoryCentralizedItemEntity> adapter1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_jicai_child, container, false);
        return binding.getRoot();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerShoppingFragComponent.builder()
                .appComponent(appComponent)
                .shoppingFragModule(new ShoppingFragModule(ShopJcChildFragment.this))
                .build()
                .inject(this);
    }

    public static ShopJcChildFragment newInstance(String companyNo) {
        ShopJcChildFragment fragment = new ShopJcChildFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Company_No, companyNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void initView() {
        String[] mTitles = new String[3];
        mTitles[0] = "需方发起";
        mTitles[1] = "供方发起";
        mTitles[2] = "企业专项";
        for (int i = 0; i < mTitles.length; i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(mTitles[i]));
        }
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().toString().equals("需方发起")){
                    topnum = 1;
                }
                if (tab.getText().toString().equals("企业专项")){
                    topnum = 2;
                }
                if (tab.getText().toString().equals("供方发起")){
                    topnum=3;
                }
                initData(topnum,false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        DividerItemDecoration divider = new DividerItemDecoration(this.getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this.getContext(), R.drawable.shape_list_divide_factory_jc));
        binding.rlvList.addItemDecoration(divider);
        binding.rlvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rlvList.setLoadingMoreEnabled(true);
        binding.rlvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData(topnum,false);
            }

            @Override
            public void onLoadMore() {
                initData(topnum,true);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        initData(topnum,false);
    }

    private void initData(int type,boolean loadMore) {
        initViewList();
        listBean = new JiCaiHomeListRequestBean();
        listBean.setCompanyNo( (String) getArguments().get(Company_No));
        if (type ==1||type==2){
            listBean.setGroupBuyMode(type+"");
            viewModel.getJiCaiList(listBean,loadMore);
        }else {
            viewModel.getGongFangList(listBean,loadMore);
        }
    }

    private void initViewList() {
        if (topnum==1||topnum ==2){
            adapter = new ShopJiCaiAdapter<CentralizedMiningEntity>(getActivity(),R.layout.item_centralized_mining2,null);
        }else {
            adapter = new ShopJiCaiAdapter<FactoryCentralizedItemEntity>(getActivity(),R.layout.item_factory_centralized_list,null);
        }
        adapter.setItemClickCallBack(new ShopJiCaiAdapter.ItemClickCallBack() {
            @Override
            public void onItemClick(int type, String no) {
                if (type==1){
                    viewModel.getH5Url(type,no);
                }else {
                    viewModel.getH5Url(type,no);
                }
            }
        });
        binding.rlvList.setAdapter(adapter);
    }

    //需方和企业专项数据展示
    public void showList(List<CentralizedMiningEntity> list, boolean isLastPage) {
        binding.rlvList.refreshComplete();
        binding.rlvList.loadMoreComplete();
        if (list.size() > 0) {
            adapter.setDatas(list);
            binding.emptyView.setVisibility(View.GONE);
        } else {
            adapter.setDatas(null);
            binding.emptyView.setVisibility(View.VISIBLE);
        }
        binding.rlvList.setLoadingMoreEnabled(!isLastPage);
    }

    //需方和企业专项数据展示
    public void lodMore(List<CentralizedMiningEntity> list, boolean isLastPage) {
        binding.rlvList.refreshComplete();
        if (list.size() != 0) {
            adapter.addDatas(list);
        }
        binding.rlvList.setLoadingMoreEnabled(!isLastPage);
    }

    //供方项数据展示
    public void showGongFangList(List<FactoryCentralizedItemEntity> list, boolean isLastPage) {
        binding.rlvList.refreshComplete();
        binding.rlvList.loadMoreComplete();
        if (list.size() > 0) {
            adapter.setDatas(list);
            binding.emptyView.setVisibility(View.GONE);
        } else {
            adapter.setDatas(null);
            binding.emptyView.setVisibility(View.VISIBLE);
        }
        binding.rlvList.setLoadingMoreEnabled(!isLastPage);

    }
    //供方项数据展示
    public void lodGongFangMore(List<FactoryCentralizedItemEntity> list, boolean isLastPage) {
        binding.rlvList.refreshComplete();
        if (list.size() != 0) {
            adapter.addDatas(list);
        }
        binding.rlvList.setLoadingMoreEnabled(!isLastPage);
    }

    //详情跳转
    public void showUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }
}
